#include <QTimer>
#include "Preferences.h"
#include "ui_Preferences.h"
#include "LoginDialog.h"
#include "EditAccountDialog.h"

Preferences::Preferences(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Preferences)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Tool);
    QRect frect = frameGeometry();
    frect.moveCenter(QDesktopWidget().availableGeometry().center());
    move(frect.topLeft());

    QTimer::singleShot(0,this, SLOT(MakeAccountsList()));
}

Preferences::~Preferences()
{
    delete ui;
}


void Preferences::MakeAccountsList()
{
    ui->AccountsList->setColumnWidth(0,402);

    int rows = ui->AccountsList->rowCount();
    for(int i=0;i<rows;i++)
    {
        ui->AccountsList->removeRow(0);
    }

    QStringList list = set.AccountsList();
    for(int i=0;i<list.size();i++)
    {
        ui->AccountsList->insertRow(i);
        ui->AccountsList->setItem(i,0,new QTableWidgetItem(list.at(i)));
        QTableWidgetItem *NotificationCheckBox = new QTableWidgetItem();
        if(set.GetValueOfKey(QString("Account/").append(list.at(i)).append("/notifications")).toBool())
            NotificationCheckBox->setCheckState(Qt::Checked);
        else
            NotificationCheckBox->setCheckState(Qt::Unchecked);

        ui->AccountsList->setItem(i,1,NotificationCheckBox);
     }
    if(0 == ui->AccountsList->rowCount())
    {
        ui->EditAccountButton->setDisabled(true);
        ui->DelAccountButton->setDisabled(true);
    }
    else{
        ui->EditAccountButton->setDisabled(false);
        ui->DelAccountButton->setDisabled(false);
    }
}

void Preferences::on_AddAccountButton_clicked()
{

    LoginDialog l;
    connect(&l,SIGNAL(CreateDatabase(QString)),this,SIGNAL(CreateDatabase(QString)));
    if(QDialog::Accepted == l.exec())
        this->MakeAccountsList();
}

void Preferences::on_DelAccountButton_clicked()
{
    QList<QTableWidgetItem*> email = ui->AccountsList->selectedItems();
    if(!email.isEmpty() && !email.at(0)->text().isEmpty())
    {
        QString key = QString("Account/").append(email.at(0)->text());
        set.DelKey(key);
        set.SetValueForKey(SettingsKeys::KEY_COUNT_ACCOUNT,set.GetValueOfKey(SettingsKeys::KEY_COUNT_ACCOUNT).toInt()-1);
        emit DeleteDatabase(email.at(0)->text());
        this->MakeAccountsList();
    }
}


void Preferences::on_AccountsList_cellChanged(int row, int column)
{
    if(1 == column)
    {
        QString email = ui->AccountsList->item(row,0)->text();
        QString key = QString("Account/").append(email).append("/notifications");
        set.SetValueForKey(key, ui->AccountsList->item(row,1)->checkState());
    }
}

void Preferences::on_EditAccountButton_clicked()
{
    QList<QTableWidgetItem*> email = ui->AccountsList->selectedItems();
    if(!email.isEmpty())
    {
        EditAccountDialog e;
        e.Edit(email.at(0)->text());
        if(QDialog::Accepted == e.exec())
            this->MakeAccountsList();
    }
}
